package com.cskaoyan.service;

import com.cskaoyan.mapper.AdvertiseMapper;
import com.cskaoyan.mapper.StorageMapper;
import com.cskaoyan.model.bean.Storage;
import com.cskaoyan.model.bean.advertise.Advertise;
import com.cskaoyan.model.bean.advertise.AdvertiseExample;
import com.cskaoyan.model.bo.extend.ListBo;
import com.cskaoyan.model.vo.extend.ListVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author：Chunk
 * @Description:
 * @Date Created in 23:14 2021/4/8
 */
@Service
public class AdvertiseServiceImpl implements AdvertiseService {
    @Autowired
    AdvertiseMapper advertiseMapper;
    @Autowired
    StorageMapper storageMapper;

    @Override
    public ListVo query(ListBo listBo) {
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());

        AdvertiseExample advertiseExample = new AdvertiseExample();
        AdvertiseExample.Criteria criteria = advertiseExample.createCriteria();
        if(listBo.getName() != null && !"".equals(listBo.getName())){
            criteria.andNameLike("%" + listBo.getName() + "%");
        }
        if(listBo.getContent() != null && !"".equals(listBo.getContent())){
            criteria.andContentLike("%" + listBo.getContent() + "%");
        }
        advertiseExample.setOrderByClause(listBo.getSort() + " " +listBo.getOrder());
        List<Advertise> advertises = advertiseMapper.selectByExample(advertiseExample);

        PageInfo<Advertise> advertisePageInfo = new PageInfo<>(advertises);
        long total = advertisePageInfo.getTotal();
        ListVo listVo = new ListVo();
        listVo.setTotal(total);
        listVo.setItems(advertises);
        return listVo;
    }

    @Override
    public Advertise update(Advertise advertise) {
        advertiseMapper.updateByPrimaryKey(advertise);
        Advertise advertiseVo = advertiseMapper.selectByPrimaryKey(advertise.getId());
        return advertiseVo;
    }

    @Override
    public void delete(Advertise advertise) {
        advertise.setDeleted(true);
        advertiseMapper.updateByPrimaryKeySelective(advertise);
    }

    @Override
    public Advertise adCreate(Advertise advertise) {
        advertiseMapper.insertSelective(advertise);
        Advertise advertiseVo = advertiseMapper.selectByPrimaryKey(advertise.getId());
        return advertiseVo;
    }

    @Override
    public Storage storageCreate(MultipartFile file) {
//        String name = file.getName();//请求参数名 👉 file
        Storage storage = new Storage();
        String originalFilename = file.getOriginalFilename();
        File path = new File("d:\\mall28\\storage");
        if (!path.exists()) {
            boolean mkdirs = path.mkdirs();
        }
        //构造一个file来接收multipartFile
        File filepath = new File(path, originalFilename);//你想要以什么文件保存，第二个参数就写什么
        try {
            //将multipartFile转换为file,保存起来
            file.transferTo(filepath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        storage.setKey(originalFilename);
        storage.setName(originalFilename);
        storage.setType(file.getContentType());
        storage.setSize((int) file.getSize());
        storage.setUrl("http://localhost:8083/mall28/storage/" + originalFilename);
        storageMapper.insertSelective(storage);
        Storage storageVo = storageMapper.selectByPrimaryKey(storage.getId());
        return storageVo;
    }


}
